package com.fr.lintcode;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

/**
*作者：furong
*日期：2017年2月7日
*时间：下午7:10:49
*/
public class Q124 {
    /**
     * @param nums: A list of integers
     * @return an integer
     */
    public int longestConsecutive(int[] num) {
        if (num == null || num.length <= 0) {
            return 0;
        }
        int max = 1;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i : num) {
            map.put(i, 1);
        }
        for (int i : num) {
            Integer c = map.get(i);
            int t = i + 1;
            if (c == 1) {
                Integer tmp = map.get(t);
                while (tmp != null) {
                    if (tmp == 1) {
                        c += tmp;
                    } else {
                        c += tmp;
                        break;
                    }
                    t++;
                    tmp = map.get(t);
                }
                for (int j = i; j < t; j++) {
                    map.put(j, c);
                }
            }
            if (c > max) {
                max = c;
            }
        }
        return max;
    }

    @Test
    public void testA() {
        int num[] = { 100, 4, 200, 1, 3, 2, 2 };
        System.out.println(longestConsecutive(num));
    }
}
